package com.example.yijian.edit.view.popup;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.yijian.R;
import com.example.yijian.edit.background.BackgroundBlurInfo;
import com.example.yijian.edit.background.BackgroundStyleInfo;
import com.example.yijian.edit.background.SpaceItemDecoration;
import com.example.yijian.utils.ScreenUtils;
import com.example.yijian.utils.ToastUtil;
import com.example.yijian.utils.datainfo.ClipInfo;

import java.util.ArrayList;
import java.util.List;

public class CanvasVaguePopup extends BaseCommonPopupWindow {

    private final int MAX_BLUR_STRENGTH = 50;//模糊最大值
    private final static int GRADE_COUNT = 4;
    private BackgroundBlurAdapter mBackgroundBlurAdapter;
    private List<BackgroundBlurInfo> mData;


    private TextView tvUserAll;
    private RecyclerView mRecyclerView;
    private ImageView ivFinish;
    private boolean userForAll;

    private int mOnSelectPosition;
    private OnBackgroundBlurItemClickListener mOnBackgroundBlurItemClickListener;

    public CanvasVaguePopup(Context context) {
        super(context);
        setContentView(R.layout.popup_canvas_vague);
        initView();
    }

    private void initView() {
        tvUserAll = findViewById(R.id.tv_canvas_use_all);
        mRecyclerView = findViewById(R.id.recyclerView);
        ivFinish = findViewById(R.id.iv_canvas_vague_finish);
        ivFinish.setOnClickListener(v -> dismiss());
        tvUserAll.setOnClickListener(v -> {
            userForAll = true;
            if (mOnBackgroundBlurItemClickListener != null) {
                ToastUtil.showToast(getContext(), "已应用到全部片段");
                mOnBackgroundBlurItemClickListener.onBlurItemClick(userForAll, getStrength(mOnSelectPosition));
            }
        });
    }

    public void setData(ClipInfo clipInfo) {
        mData = new ArrayList<>();
        for (int i = 1; i < GRADE_COUNT + 1; i++) {
            BackgroundBlurInfo backgroundBlurInfo = new BackgroundBlurInfo(i + "");
            mData.add(backgroundBlurInfo);
        }
        mData.add(0, new BackgroundBlurInfo(getContext().getResources().getString(R.string.background_blur_no)));
        initRecyclerView();
        int position = -1;
        for (int i = 0; i < mData.size(); i++) {
            BackgroundBlurInfo multiColorInfo = mData.get(i);
            if (multiColorInfo == null) {
                continue;
            }
            float strength = getStrength(i);
            if (String.valueOf(strength).equals(clipInfo.getBackgroundValue())) {
                position = i;
                break;
            }
        }
        if (position == mOnSelectPosition) {
            return;
        }
        int oldPosition = mOnSelectPosition;
        mOnSelectPosition = position;
        mBackgroundBlurAdapter.notifyItemChanged(oldPosition);
        mBackgroundBlurAdapter.notifyItemChanged(mOnSelectPosition);
    }

    private void initRecyclerView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
        mBackgroundBlurAdapter = new BackgroundBlurAdapter();
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setAdapter(mBackgroundBlurAdapter);
        mRecyclerView.addItemDecoration(new SpaceItemDecoration(ScreenUtils.dip2px(getContext(), 3), ScreenUtils.dip2px(getContext(), 12)));
    }

    private class BackgroundBlurAdapter extends RecyclerView.Adapter<BlurHolder> {

        @Override
        public BlurHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            View view = null;
            view = LayoutInflater.from(getContext()).inflate(R.layout.item_background_blur, null);
            return new BlurHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull BlurHolder blurHolder, int i) {
            final int position = i;
            BackgroundBlurInfo backgroundBlurInfo = mData.get(i);
            if (backgroundBlurInfo == null) {
                return;
            }

            if (getContext().getResources().getString(R.string.background_blur_no).equals(backgroundBlurInfo.getName())) {
                blurHolder.mRlContent.setBackgroundResource(R.mipmap.ic_blur_no);
                blurHolder.mTvContent.setVisibility(View.GONE);
            } else {
                blurHolder.mRlContent.setBackgroundResource(R.mipmap.ic_blur_strength_bg);
                blurHolder.mTvContent.setVisibility(View.VISIBLE);
                blurHolder.mTvContent.setText(backgroundBlurInfo.getName());
            }

            blurHolder.itemView.setOnClickListener(view -> {
                if (mOnBackgroundBlurItemClickListener != null) {
                    mOnBackgroundBlurItemClickListener.onBlurItemClick(userForAll, getStrength(position));
                }
                if (mOnSelectPosition == position) {
                    return;
                }
                notifyItemChanged(mOnSelectPosition);
                mOnSelectPosition = position;
                notifyItemChanged(mOnSelectPosition);
            });

            blurHolder.mMask.setVisibility(mOnSelectPosition == position ? View.VISIBLE : View.GONE);

        }

        @Override
        public int getItemCount() {
            return mData == null ? 0 : mData.size();
        }

    }


    private class BlurHolder extends RecyclerView.ViewHolder {

        private TextView mTvContent;
        private RelativeLayout mRlContent;
        private View mMask;

        public BlurHolder(@NonNull View itemView) {
            super(itemView);
            mTvContent = itemView.findViewById(R.id.tv_content);
            mRlContent = itemView.findViewById(R.id.rl_content);
            mMask = itemView.findViewById(R.id.mask);
        }
    }


    public float getStrength(int position) {
        float strength = position * 1.0F / GRADE_COUNT;
        strength *= MAX_BLUR_STRENGTH;
        return strength;
    }

    public List<BackgroundBlurInfo> getBlurData() {
        return mData;
    }

    public void setOnBackgroundBlurItemClickListener(OnBackgroundBlurItemClickListener onBackgroundBlurItemClickListener) {
        this.mOnBackgroundBlurItemClickListener = onBackgroundBlurItemClickListener;
    }


    public interface OnBackgroundBlurItemClickListener {

        /**
         * 模糊点击回调
         *
         * @param strength 模糊强度
         */
        void onBlurItemClick(boolean useAll, float strength);

    }

}
